﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanManagement.BusinessLogic;
using HumanManagement.BusinessObjects;
using System.Data;
using HumanManagement.DataAccess;

namespace BSC_HumanManagement.Usercontrols
{
    public partial class uc_HoSoNhanVien_Detail : System.Web.UI.UserControl
    {
        int itemID = 0;
        PersonalProfile _objPersonalProfile = new PersonalProfile();
        PersonalProfileBL objPersonalProfileDA = new PersonalProfileBL();
        //public bool permissionAddNew = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtDob.MaxDate = DateTime.Now;
            this.txtDob.MinDate = Convert.ToDateTime("01/01/1900");
            
            BranchBL _objBranchBL = new BranchBL();
            foreach (DataRow item in _objBranchBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlBranchID.Items.Add(new ListItem(item["BranchName"].ToString(),item["ID"].ToString()));
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Hồ sơ nhân viên Detail")
            {
                LoadData();
                DisableControl();                               
                ShowData(_objPersonalProfile);
            }
            if (Request.QueryString["ID"] == null && Request.QueryString["FunctionName"] == "Hồ sơ nhân viên Detail")
            {
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Hồ sơ nhân viên Edit")
            {
                if (LoadData())
                {
                    ShowData(_objPersonalProfile);
                    this.btnDelete.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hồ sơ nhân viên mã ID: " + Request.QueryString["ID"] + 
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Hồ sơ nhân viên Delete")
            {
                if (LoadData())
                {
                    ShowData(_objPersonalProfile);
                    this.btnSave.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hồ sơ nhân viên mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
        }

        private void ShowData(PersonalProfile _objPersonalProfile)
        {
            for (int i = 0; i < this.ddlBranchID.Items.Count; i++)
            {
                if (this.ddlBranchID.Items[i].Value == _objPersonalProfile.BranchID.ToString())
                {
                    this.ddlBranchID.SelectedIndex = i;
                }
            }
            if (_objPersonalProfile.Sex == "1")
            {
                this.ddlSex.SelectedIndex = 0;
            }
            else
            {
                this.ddlSex.SelectedIndex = 1;
            }
            this.txtFullName.Text = Convert.ToString(_objPersonalProfile.FullName);
            this.txtDob.SelectedDate = _objPersonalProfile.DateOfBirth;
            this.txtFamilyRegisterAddress.Text = Convert.ToString(_objPersonalProfile.FamilyRegisterAddress);
            this.txtContactAddress.Text = Convert.ToString(_objPersonalProfile.ContactAddress);
            this.txtNativeCountry.Text = Convert.ToString(_objPersonalProfile.NativeCountry);
            this.txtIdentificationNumber.Text = Convert.ToString(_objPersonalProfile.IdentificationNumber);
            this.txtSocialInsuranceNumber.Text = Convert.ToString(_objPersonalProfile.SocialInsuranceNumber);
            this.txtDescription.Text = Convert.ToString(_objPersonalProfile.Description);
            this.txtEmail.Text = Convert.ToString(_objPersonalProfile.Email);
            if (_objPersonalProfile.Picture != null)
            {
                string base64String = Convert.ToBase64String(_objPersonalProfile.Picture, 0, _objPersonalProfile.Picture.Length);
                this.imgPicture.ImageUrl = "data:image/jpeg;base64," + base64String;
            }
        }

        private void DisableControl()
        {
            this.ddlSex.Enabled = false;
            this.ddlBranchID.Enabled = false;
            this.ddlNationalityID.Enabled = false;
            this.ddlReligionID.Enabled = false;
            this.txtFullName.Enabled = false;
            this.txtDob.Enabled = false;
            this.txtFamilyRegisterAddress.Enabled = false;
            this.txtContactAddress.Enabled = false;
            this.txtNativeCountry.Enabled = false;
            this.txtIdentificationNumber.Enabled = false;
            this.txtNativeCountry.Enabled = false;
            this.txtSocialInsuranceNumber.Enabled = false;
            this.txtDescription.Enabled = false;
            this.txtEmail.Enabled = false;
            this.fileUploadPicture.Enabled = false;
        }

        private bool LoadData()
        {
            itemID = Convert.ToInt32(Request.QueryString["ID"]);
            objPersonalProfileDA = new PersonalProfileBL();
            _objPersonalProfile = objPersonalProfileDA.GetByID(itemID, Convert.ToInt32(Session["PersonalProfileID"].ToString()));
            if (_objPersonalProfile == null)
            {
                return false;
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this._objPersonalProfile.BranchID = Convert.ToInt16(this.ddlBranchID.SelectedValue);
            this._objPersonalProfile.ContactAddress = this.txtContactAddress.Text;
            this._objPersonalProfile.DateOfBirth = Convert.ToDateTime(this.txtDob.SelectedDate);
            this._objPersonalProfile.Description = this.txtDescription.Text;
            this._objPersonalProfile.Email = this.txtEmail.Text;
            this._objPersonalProfile.FamilyRegisterAddress = this.txtFamilyRegisterAddress.Text;
            this._objPersonalProfile.FullName = this.txtFullName.Text;
            this._objPersonalProfile.IdentificationNumber = this.txtIdentificationNumber.Text;
            this._objPersonalProfile.LastDate = DateTime.Now;
            this._objPersonalProfile.NationalityID = 1;
            this._objPersonalProfile.NativeCountry = this.txtNativeCountry.Text;
            this._objPersonalProfile.ProfileStatus = "1";
            this._objPersonalProfile.ReligionID = 1;
            this._objPersonalProfile.Sex = this.ddlSex.SelectedValue;
            this._objPersonalProfile.SocialInsuranceNumber = this.txtSocialInsuranceNumber.Text;
            if (this.fileUploadPicture.HasFile )
            {
                if (this.fileUploadPicture.FileBytes.Length < 51200)
                {
                    byte[] Image = null;
                    Image = new byte[fileUploadPicture.PostedFile.ContentLength];
                    HttpPostedFile UploadedImage = fileUploadPicture.PostedFile;
                    UploadedImage.InputStream.Read(Image, 0, (int)fileUploadPicture.PostedFile.ContentLength);
                    this._objPersonalProfile.Picture = Image;    
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Ảnh phải nhỏ hơn 50Kb !')", true);
                    return;
                }
            }
            
            if (Request.QueryString["ID"] == null)
            {
                byte[] data = { 0, 0 };
                _objPersonalProfile.Picture = data;
                objPersonalProfileDA.Add(_objPersonalProfile);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Tạo mới thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Hồ sơ nhân viên");
            }

            if (Request.QueryString["FunctionName"] == "Hồ sơ nhân viên Edit")
            {
                objPersonalProfileDA.Update(_objPersonalProfile);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Sửa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Hồ sơ nhân viên");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                PersonalProfileBL _objPersonalProfileDA = new PersonalProfileBL();
                PersonalProfile _objPersonalProfile = _objPersonalProfileDA.GetByID(Convert.ToInt32(Request.QueryString["ID"]), Convert.ToInt32(Session["PersonalProfileID"].ToString()));
                _objPersonalProfile.ProfileStatus = "0";
                _objPersonalProfileDA.Update(_objPersonalProfile);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Xóa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Hồ sơ nhân viên");
            }
            else { }
        }
    }
}